<%@ page import="java.util.Calendar,
java.util.GregorianCalendar,
java.text.SimpleDateFormat,
java.util.Date,
java.util.TimeZone,
java.util.ArrayList,
com.smartodo.utils.*,
java.sql.*" %>

<%

	String loginId = session.getAttribute("loginId").toString();
	// Initialize:
	GregorianCalendar cal = new GregorianCalendar();
	cal.setTimeZone(TimeZone.getTimeZone("US/Central"));
	
	// Get the current date
	int curr_day = cal.get(Calendar.DAY_OF_MONTH);
	int curr_month = cal.get(Calendar.MONTH);
	int curr_year = cal.get(Calendar.YEAR);

	// selected date
	int day = 0, month = 0 , year =0;

	// get the request parameters passed.
	if(request.getParameter("n") != null)
	{
		if(session.getAttribute("selectedDate") != null)
		{
			cal = (GregorianCalendar)session.getAttribute("selectedDate");
			cal.roll(Calendar.MONTH, 1);
			day = cal.get(Calendar.DAY_OF_MONTH);
			month = cal.get(Calendar.MONTH);
			year = cal.get(Calendar.YEAR);
		}
	}
	if(request.getParameter("p") != null)
	{
		if(session.getAttribute("selectedDate") != null)
		{
			cal = (GregorianCalendar)session.getAttribute("selectedDate");
			cal.roll(Calendar.MONTH, -1);
			day = cal.get(Calendar.DAY_OF_MONTH);
			month = cal.get(Calendar.MONTH);
			year = cal.get(Calendar.YEAR);
		}
	}
	
	
	// get the parameters from request when selected from the select box
	if(request.getParameter("d") != null)
	{
		try
		{
			day = Integer.parseInt(request.getParameter("d"));
		}
		catch(Exception e){ day = 0;}
	}
	if(day == 0) day = curr_day;
	
	if(request.getParameter("m") != null)
	{
		try
		{
			month = Integer.parseInt(request.getParameter("m"));
			month--;
		}
		catch(Exception e){ month = 0;}
	}
	if(month == 0) month = curr_month;

	if(request.getParameter("y") != null)
	{
		try
		{
			year = Integer.parseInt(request.getParameter("y"));
		}
		catch(Exception e){ year = 0;}
	}
	if(year == 0) year = curr_year;
	
	cal.set(year, month, day);
	session.setAttribute("selectedDate", cal);
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@page import="java.util.TimeZone;"%>
<html style="height:100%;width:100%" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>SMARTODO</title>
<link href="/Smartodo/css/all.css" type="text/css" rel="stylesheet"/>
<link href="/Smartodo/css/jquery-ui-1.7.1.custom.css" type="text/css" rel="stylesheet"/>

<script language="JavaScript" type="text/javascript" src="/Smartodo/javascript/jquery-1.3.2.min.js"></script>
<script type='text/javascript' src='/Smartodo/javascript/jquery-ui-1.7.1.custom.min.js'></script>
<script type="text/javascript" src="/Smartodo/javascript/main.js"></script>

</head>
<body style="height:100%;width:100%">
<div class="Header">
<table cellpadding=0 cellspacing=0 border=0 width="100%" align="center">
	<tr>
		<td width="40%">&nbsp;</td>
		<td>SMARTODO<br/><span style="font-size: 50%;">Track your tasks, Effortlessly!</span></td>
		<td width="40%" valign="bottom" align="right">
			<div style="font-size: 50%;width:400px;margin-right:10px;padding:3px;">
			<span><b>Welcome <%=session.getAttribute("loginId") %>!</b></span>
			<span> | <a href="Logout.do">Sign out</a></span>
			</div>
		</td>
	</tr>
</table>
</div>


<!-- Begin Section -->
<div class="SectionDiv" > 
<table cellpadding=0 cellspacing=0 border=0 width="100%">
<tr>
	<td align="center">
	<input type="button" class="formElements" value="Today" onclick="displayToday()"/>
	<%
	
	HtmlSelectBox selectBox = new HtmlSelectBox("mo","","");
	
	selectBox.addOption("1", "January");
	selectBox.addOption("2", "February");
	selectBox.addOption("3", "March");
	selectBox.addOption("4", "April");
	selectBox.addOption("5", "May");
	selectBox.addOption("6", "June");
	selectBox.addOption("7", "July");
	selectBox.addOption("8", "August");
	selectBox.addOption("9", "September");
	selectBox.addOption("10", "October");
	selectBox.addOption("11", "November");
	selectBox.addOption("12", "December");
	selectBox.setSelected(String.valueOf(month + 1));
	out.print(selectBox.Generate() + "&nbsp;");
	
	selectBox = new HtmlSelectBox("yr","","");
	selectBox.addOption("2009", "2009");
	selectBox.addOption("2010", "2010");
	selectBox.addOption("2011", "2011");
	selectBox.addOption("2012", "2012");
	selectBox.setSelected(String.valueOf(year));
	out.print(selectBox.Generate());
	
	%>
	
	<input type="button" class="formElements" value=" Go " onclick="displayCalendar()"/>
	<input type="button" class="formElements" value=" << " onclick="calendarPrevMonth()"/>
	<input type="button" class="formElements" value=" >> " onclick="calendarNextMonth()"/>
	</td>
</tr>
</table>
</div>


<!-- Begin Chart Section -->
<div class="SectionDiv" > 

<table border=0 width="100%" height="100%" cellpadding=0 cellspacing=5 align="center">
<tr valign="top">
<td>
<div class="calendarArea">
	<div class="calendarHeader" style="text-align:center">
		<b style="font-size:130%">
		<%
			SimpleDateFormat sdf = new SimpleDateFormat("MMMM yyyy");
			out.print(sdf.format(cal.getTime()));
		%></b>
	</div>
	<table class="calendarBody" border=0 cellpadding=0 cellspacing=0>
	<tr>
	<td>
	<!-- Begin: Render the Calendar -->
	<%
		// There are things we need to know about this month such as the last day:
		int last = cal.getActualMaximum(Calendar.DAY_OF_MONTH);

		// We also need to know what day of the week the first day is,
		Calendar temp_cal = new GregorianCalendar();
		temp_cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), 1);
		int firstwday = temp_cal.get(Calendar.DAY_OF_WEEK);// 1=> Sunday, 2=> Monday, 3 => Wednesday

	%>
	<table class='calendarTable' cellpadding=0 cellspacing=0 height='100%' width="100%">
	<tr>
		<th height="30">Sun</th>
		<th height="30">Mon</th>
		<th height="30">Tue</th>
		<th height="30">Wed</th>
		<th height="30">Thu</th>
		<th height="30">Fri</th>
		<th height="30">Sat</th>
	</tr>
	<%
	
		// Now we need to start some counters, and do some looping:
		int onday = 0;
		boolean started = false;
		
		// While we haven't surpassed the last day of the month, loop:
		while ( onday <= last )
		{
			// Begin our next row of the table
			out.print("<tr>");
			// Now loop 1 through 7, for the days of the week
			for(int d = 1 ; d <= 7 ; d++)
			{
				// If we haven't started yet:
				if(!started)
				{
					// Does today equal the first weekday we should start on?
					if ( d == firstwday )
					{
						// Set that we have started, and increment the counter
						started = true;
						onday++;
					}
				}
				// Now if the day is zero or greater than the last day make a
				// blank table cell.
				if ( ( onday == 0 ) || ( onday > last ) )
				{
					out.print("<td class='calendarTableCell'>&nbsp;</td>");
				}
				else
				{
					// build the date
					//$format = "m/d/Y";
					//$display_date = date($format, mktime(0,0,0,$month,$onday,$year));
					
					// Check for the current date
					String item_bkg = "#ffffff";
					if(curr_day == onday && curr_month == month && curr_year == year)
						item_bkg = "#fff9d6";
					if(day == onday)
						item_bkg = "#ece9de";
		
					String onclick_event = "onclick=\"calendarDayDisplay('"+onday+"','"+(month+1)+"','"+year+"')\"";
					// Otherwise, echo out a day & Increment the counter
					out.print("<td class='calendarTableCell' valign='top'>");
					out.print("<table border=0 cellpadding=0 cellspacing=0 width='100%' height='100%'>");
					out.print("<tr><td align='right' class='calendarDay' " + onclick_event + ">" + onday + "</td></tr>");
					
					String onday_todo_out = "";
					ArrayList<STask> onday_tlist = DbUtils.getTaskForUser(loginId, onday, (month + 1), year);
					
					int nCount = onday_tlist.size(); 
					if(nCount == 0)
					{
						onday_todo_out = "&nbsp;";
					}
					else
					{
						// Display atmost 4 to do items and display "more.."
						int displayCount = 4;
						if(nCount <= 4)	displayCount = nCount;
						
						for (int i = 0; i < displayCount; i++)
						{
							STask t = onday_tlist.get(i);
							String css_class = "itemSmallList";
							if(t.priority.equals("h")) css_class = "highPen";
							if(t.priority.equals("m")) css_class = "medPen";
							if(t.priority.equals("l")) css_class = "lowPen";

							onday_todo_out += "<div class='" + css_class + "' " + onclick_event + ">" + t.task_name + "</div>";
						}
						if(nCount > 4)
						{
							onday_todo_out += "<div class='itemSmallList'> more... </div>";
						}
					}
					
					out.print("<tr><td align='left'class='calendarItem' style='background-color: " + item_bkg + "'>" + onday_todo_out + "</td></tr>");
					out.print("</table></td>");
					onday++;
				}
			}
			
			
			// End this table row:
			out.print("</tr>\n");			
		}
	
	%>
	</table>	
	<!-- End: Render the Calendar -->
	</td>
	</tr>
	</table>
</div>
</td>
<td width="250px">
	<div class="manageTaskArea" style="margin-left:5px">
		<div style="width:auto;margin-bottom:5px;" class="manageTaskHeader"> 
		<b style="font-size:130%">
		<%
			SimpleDateFormat sdf_task = new SimpleDateFormat("EEEE MMMM d");
			out.print(sdf_task.format(cal.getTime()));
		%>
		</b><br/>
		<span style="font-size:80%">Click an event below to make changes.</span>
		</div>
		
		<div style="width:auto;margin-bottom:10px;" class="manageTaskHeader"> 
		<%
		ArrayList<STask> tlist = DbUtils.getTaskForUser(loginId, day, month + 1, year);
		
		if(tlist.size() == 0)
		{
			out.print("<div align='center'><b>---</b></div>");
		}
		else
		{
			for (int i = 0; i < tlist.size(); i++)
			{
				STask t = tlist.get(i);
				String css_class = "itemList";
				if(t.priority.equals("h")) css_class = "highDetailPen";
				if(t.priority.equals("m")) css_class = "medDetailPen";
				if(t.priority.equals("l")) css_class = "lowDetailPen";
				
				out.print("<div class='" + css_class + "' onclick=\"javascript: showUpdateTask('"+t.taskid+"')\">"+t.task_name+"</div>");
			}
		}
		%>
		</div>
		
		<div id="newTaskDiv" style="width:auto;" class="manageTaskHeader"> 
			<div style="font-size:110%;padding-bottom:5px;font-weight: bold;">Add a Task</div>
	
			<div style="padding-bottom:5px;margin-bottom:5px;border-bottom: gray thin dotted;">
				<input id="newtaskname" name="newtaskname" style="width:97%" class="inputtext" value="" />
				<span style="font-size:80%">e.g. "Lunch with Susan"</span>
				<input type="hidden" id="newtaskdate" name="newtaskdate" style="display:none" value="<% out.print((month + 1) + "/" + day + "/" + year); %>"/>
			</div>
	
			<div style="padding:5px 5px 5px 10px;margin-bottom:5px;border-bottom: gray thin dotted;">
				Priority &nbsp;
				<select id="newpriority" name="pr" style="width:60%" >
				<option value="h">High</option>
				<option value="m">Medium</option>
				<option value="l">Low</option>
				</select>
			</div>
	
			<div><input type="button" class="formElements" value="Add Task" onclick="addTask()"/></div>
		</div>
		
		<div id="updateTaskDiv" style="width:auto;display:none;" class="manageTaskHeader"> 
			<div style="font-size:110%;padding-bottom:5px;font-weight: bold;">Update Task</div>
	
			<div style="padding-bottom:5px;margin-bottom:5px;border-bottom: gray thin dotted;">
				<input type="hidden" id="taskid" name="taskid" style="display:none" value=""/>
				<input id="updatetaskname" name="updatetaskname" style="width:97%" class="inputtext" value="" />
			</div>
	
			<div style="padding:5px 5px 5px 10px;margin-bottom:5px;border-bottom: gray thin dotted;">
				Change Date &nbsp;
				<input id="taskdatepicker" name="taskdatepicker" style="width:80px;" class="inputtext" value="" disabled/>
			</div>

			<div style="padding:5px 5px 5px 10px;margin-bottom:5px;border-bottom: gray thin dotted;">
				Priority &nbsp;
				<select id="updatepriority" name="updatepriority" style="width:60%" >
				<option value="h">High</option>
				<option value="m">Medium</option>
				<option value="l">Low</option>
				</select>
			</div>
	
			<div>
			<input type="button" class="formElements" value="Update" onclick="updateTask()"/>
			<input type="button" class="formElements" value="Delete" onclick="deleteTask()"/>
			<input type="button" class="formElements" value="Cancel" onclick="cancelUpdate()"/>
			</div>
		</div>
		
		
	</div>

	<div style="margin-top:10px;padding:5px">
		<table border=0 cellpadding=0 cellspacing=10 style="font-size:11px">
		<tr>
		<td class="highColor" width="12px">&nbsp;</td><td>High Priority</td>
		</tr>
		<tr>
		<td class="medColor">&nbsp;</td><td>Medium Priority</td>
		</tr>
		<tr>
		<td class="lowColor">&nbsp;</td><td>Low Priority</td>
		</tr>
		</table>
	</div>	
</td>
</tr>
</table>
</div>


<div class="footer">
Secure Programming<br/>
Department of Computer Science and Engineering, <b>The University of Texas at Arlington </b>, Arlington, Texas 76019-0015
</div>

</body>
</html>